package service

import (
	"encoding/json"
	"github.com/gin-gonic/gin"
	"math/rand"
	"net/http"
	"strconv"
	"time"
	"wechats/basic/config"
	"wechats/handler/Request"
	"wechats/handler/model"
	"wechats/pkg"
)

func SendSms(c *gin.Context) {
	var form Request.SendSms
	// 根据 Content-Type Header 推断使用哪个绑定器。
	if err := c.ShouldBind(&form); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"Code": 400, "Msg": "参数错误"})
		return
	}
	count, _ := config.Rdb.Get(config.Ctx, "send"+form.Phone).Int64()
	if count >= 3 {
		c.JSON(http.StatusBadRequest, gin.H{"Code": 400, "Msg": "验证码1分钟最多发送3次"})
		return
	}

	code := rand.Intn(900000) + 100000
	sms, err := pkg.SendSms(form.Phone, strconv.Itoa(code))
	if err != nil {
		return
	}
	if sms.Code != 2 {
		c.JSON(http.StatusBadRequest, gin.H{"Code": 400, "Msg": "验证码发送失败"})
		return
	}
	config.Rdb.Set(config.Ctx, "sendKey"+form.Phone, code, time.Hour*2)
	config.Rdb.Incr(config.Ctx, "send"+form.Phone)
	config.Rdb.Expire(config.Ctx, "send"+form.Phone, time.Minute*1)
	c.JSON(http.StatusOK, gin.H{"Code": 400, "Msg": "验证码发送成功"})
}

func Register(c *gin.Context) {
	var form Request.Register
	// 根据 Content-Type Header 推断使用哪个绑定器。
	if err := c.ShouldBind(&form); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"Code": 400, "Msg": "参数错误"})
		return
	}
	count, _ := config.Rdb.Get(config.Ctx, "sendKey"+form.Account).Result()
	if count != form.Sends {
		c.JSON(http.StatusBadRequest, gin.H{"Code": 400, "Msg": "验证码不正确,请重试"})
	}
	var user model.User
	user.Account = form.Account
	user.Password = pkg.Md5(form.Password)
	user.Signature = form.Signature
	user.Nickname = form.Nickname
	user.Avatar = form.Avatar
	err := user.UserAdd(config.DB)
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"Code": 400, "Msg": "注册失败,请重试"})
		return
	}

	c.JSON(http.StatusOK, gin.H{"Code": 400, "Msg": "用户注册成功"})
}
func Login(c *gin.Context) {
	var form Request.Login
	// 根据 Content-Type Header 推断使用哪个绑定器。
	if err := c.ShouldBind(&form); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"Code": 400, "Msg": "参数错误"})
		return
	}

	var user model.User

	if user.Password == pkg.Md5(form.Password) {
		c.JSON(http.StatusBadRequest, gin.H{"Code": 400, "Msg": "密码不正确,请重试"})
		return
	}
	err := user.UserAdd(config.DB)
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"Code": 400, "Msg": "登录失败,请重试"})
		return
	}
	token, err := pkg.GetJwtToken(strconv.Itoa(int(user.ID)))
	if err != nil {
		return
	}
	json.Marshal(&user)
	config.Rdb.Set(config.Ctx, "Keys", user, time.Minute*30)
	c.JSON(http.StatusOK, gin.H{"Code": 400, "Msg": "用户登录成功", "data": token})
}
func FriendSList(c *gin.Context) {
	var form Request.FriendList
	// 根据 Content-Type Header 推断使用哪个绑定器。
	if err := c.ShouldBind(&form); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"Code": 400, "Msg": "参数错误"})
		return
	}

	var user model.User
	list, err := user.FriendSList(config.DB, form.Account, form.Nickname)
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"Code": 400, "Msg": "查询好友失败,请重试"})
		return
	}
	json.Marshal(&list)
	config.Rdb.Set(config.Ctx, "Keys", list, time.Minute*30)
	c.JSON(http.StatusOK, gin.H{"Code": 400, "Msg": "查询好友成功", "data": list})
}

func FriendAdd(c *gin.Context) {
	var form Request.FriendAdd
	// 根据 Content-Type Header 推断使用哪个绑定器。
	if err := c.ShouldBind(&form); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"Code": 400, "Msg": "参数错误"})
		return
	}
	var friend model.Friend
	friend.UserId = form.UserId
	friend.Text = form.Text
	friend.Status = form.Status
	friend.FriendId = form.FriendId
	err := friend.FriendAdd(config.DB)
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"Code": 400, "Msg": "好友申请发送失败,请重试"})
		return
	}
	c.JSON(http.StatusOK, gin.H{"Code": 400, "Msg": "好友申请发送成功"})
}
func FriendUpdate(c *gin.Context) {
	var form Request.FriendUpdate
	// 根据 Content-Type Header 推断使用哪个绑定器。
	if err := c.ShouldBind(&form); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"Code": 400, "Msg": "参数错误"})
		return
	}
	var friend model.Friend
	err := friend.FriendUpdate(config.DB, strconv.Itoa(form.UserId), strconv.Itoa(form.Status))
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"Code": 400, "Msg": "好友申请已处理失败,请重试"})
		return
	}
	c.JSON(http.StatusOK, gin.H{"Code": 400, "Msg": "好友申请已处理成功"})
}
func FriendList(c *gin.Context) {
	var form Request.FriendList
	// 根据 Content-Type Header 推断使用哪个绑定器。
	if err := c.ShouldBind(&form); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"Code": 400, "Msg": "参数错误"})
		return
	}
	var friend model.Friend
	list, err := friend.FriendList(config.DB)
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"Code": 400, "Msg": "查询好友列表失败,请重试"})
		return
	}
	json.Marshal(list)
	config.Rdb.Set(config.Ctx, "Keys", list, time.Minute*30)
	c.JSON(http.StatusOK, gin.H{"Code": 400, "Msg": "查询好友列表成功", "data": list})
}

func BlackAdd(c *gin.Context) {
	var form Request.BlackAdd
	// 根据 Content-Type Header 推断使用哪个绑定器。
	if err := c.ShouldBind(&form); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"Code": 400, "Msg": "参数错误"})
		return
	}
	var black model.BlackD
	black.UserId = form.UserId
	black.FriendId = form.FriendId
	err := black.BlackDAdd(config.DB)
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"Code": 400, "Msg": "好友加入黑名单失败,请重试"})
		return
	}
	c.JSON(http.StatusOK, gin.H{"Code": 400, "Msg": "好友加入黑名单成功"})
}

func BlackDList(c *gin.Context) {
	var form Request.BlackDList
	// 根据 Content-Type Header 推断使用哪个绑定器。
	if err := c.ShouldBind(&form); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"Code": 400, "Msg": "参数错误"})
		return
	}

	var user model.User
	list, err := user.BlackDList(config.DB, form.UserId)
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"Code": 400, "Msg": "黑名单查询列表失败,请重试"})
		return
	}

	c.JSON(http.StatusOK, gin.H{"Code": 400, "Msg": "黑名单查询列表成功", "data": list})
}
func MomentsAdd(c *gin.Context) {
	var form Request.MomentsAdd
	// 根据 Content-Type Header 推断使用哪个绑定器。
	if err := c.ShouldBind(&form); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"Code": 400, "Msg": "参数错误"})
		return
	}
	var moments model.Moments
	moments.UserId = form.UserId
	moments.Text = form.Text
	err := moments.MomentsAdd(config.DB)
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"Code": 400, "Msg": "朋友圈动态发布失败,请重试"})
		return
	}
	c.JSON(http.StatusOK, gin.H{"Code": 400, "Msg": "朋友圈动态发布成功"})
}

func MomentsList(c *gin.Context) {
	var form Request.MomentsList
	// 根据 Content-Type Header 推断使用哪个绑定器。
	if err := c.ShouldBind(&form); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"Code": 400, "Msg": "参数错误"})
		return
	}
	var moments model.Moments
	list, err := moments.MomentsList(config.DB)
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"Code": 400, "Msg": "朋友圈查询失败,请重试"})
		return
	}

	c.JSON(http.StatusOK, gin.H{"Code": 400, "Msg": "朋友圈查询成功", "data": list})
}
func LikesAdd(c *gin.Context) {
	var form Request.LikesAdd
	// 根据 Content-Type Header 推断使用哪个绑定器。
	if err := c.ShouldBind(&form); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"Code": 400, "Msg": "参数错误"})
		return
	}
	var likes model.Likes
	likes.UserId = form.UserId
	likes.MomentsId = form.MomentsId
	likes.Like = form.Like
	err := likes.LikesAdd(config.DB)
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"Code": 400, "Msg": "朋友圈动态点赞失败,请重试"})
		return
	}
	c.JSON(http.StatusOK, gin.H{"Code": 400, "Msg": "朋友圈动态点赞发布成功"})
}
func LiaoList(c *gin.Context) {
	var form Request.LiaoList
	// 根据 Content-Type Header 推断使用哪个绑定器。
	if err := c.ShouldBind(&form); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"Code": 400, "Msg": "参数错误"})
		return
	}
	var liao model.Liao
	list, err := liao.LiaoList(config.DB)
	if err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"Code": 400, "Msg": "聊天记录查询失败,请重试"})
		return
	}

	c.JSON(http.StatusOK, gin.H{"Code": 400, "Msg": "聊天记录查询成功", "data": list})
}
